package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.domain.ProductClassify;
import cn.wolfcode.crm.domain.Shop;
import cn.wolfcode.crm.query.ProductQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.serivce.IClassifyChildService;
import cn.wolfcode.crm.serivce.IProductClassifyService;
import cn.wolfcode.crm.serivce.IProductService;
import cn.wolfcode.crm.serivce.IShopService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService service;
    @Autowired
    private IProductClassifyService productClassifyService;
    @Autowired
    private IClassifyChildService classifyChildService;
    @Autowired
    private IShopService shopService;

    @RequestMapping("view")
    public String view() throws Exception {
        return "product";
    }

    @RequestMapping("listAll")
    @ResponseBody
    public List<Product> listAll(ProductQueryObject qo) throws Exception {
        List<Product> products = service.selectAll();
        return products;
    }
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(ProductQueryObject qo) throws Exception {
        PageResult query = service.query(qo);
        return query;
    }
    @RequestMapping("count")
    @ResponseBody
    public Long count()throws Exception{
        return service.count();
    }
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Product pro) throws Exception {
        try {

            service.saveOrUpdate(pro);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("保存失败!");
        }
    }
    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse response) throws Exception {
        //设置下载响应头
        response.setHeader("Content-Disposition","attachment;filename=product.xls");
        //创建excel文件
        Workbook wb = new HSSFWorkbook();
        //创建工作薄
        Sheet sheet = wb.createSheet("day01");
        //查询员工数据
        List<Product> products = service.selectAll();
        //创建表格第一行
        Row row = sheet.createRow(0);
        //设置标题
        row.createCell(0).setCellValue("商品名称");
        row.createCell(1).setCellValue("商品编码");
        row.createCell(2).setCellValue("商品进价");
        row.createCell(3).setCellValue("销售单价");
        row.createCell(4).setCellValue("一级类别");
        row.createCell(5).setCellValue("所属店铺");

        //设置第二行开始
        for(int i=0;i< products.size();i++){
            //从第二行开始创建行
            Row row1 = sheet.createRow(i + 1);
            //从列表的第一开始取出所有的员工对象
            Product product = products.get(i);
            //创建单元格
            row1.createCell(0).setCellValue(product.getName());
            row1.createCell(1).setCellValue(product.getSn());
            row1.createCell(2).setCellValue(product.getCostPrice().toString());
            row1.createCell(3).setCellValue(product.getSalePrice().toString());
            row1.createCell(4).setCellValue(product.getClassify().getClassname());
            row1.createCell(5).setCellValue(product.getShop().getName());
        }
        //输出到浏览器
        wb.write(response.getOutputStream());
    }
    @RequestMapping("importXls")
    public void importXls(MultipartFile file) throws Exception {
        //获取excel文件
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        //获取工作簿
        Sheet sheet = wb.getSheet("day01");
        //获取总行数
        int lastRowNum = sheet.getLastRowNum();
        for(int i=1;i<=lastRowNum;i++){
            Row row = sheet.getRow(i);
            Product product = new Product();
            product.setName(row.getCell(0).getStringCellValue());
            product.setSn(row.getCell(1).getStringCellValue());
            product.setCostPrice(new BigDecimal(row.getCell(2).getStringCellValue()));
            product.setSalePrice(new BigDecimal(row.getCell(3).getStringCellValue()));
            ProductClassify productClassify = new ProductClassify();
            productClassify.setId(1L);
            product.setClassify(productClassify);
            Shop shop = new Shop();
            shop.setId(1L);
            product.setShop(shop);
            service.saveOrUpdate(product);
        }
    }

    @ResponseBody
    @RequestMapping("money")
    public Map<String,Object> money() throws Exception {
        Map<String,Object> map=service.money();
        return map;
    }
}